package cn.house.houseai.controller.user;

import cn.house.houseai.entify.Device;
import cn.house.houseai.entify.Sensors;
import cn.house.houseai.service.DeviceService;
import cn.house.houseai.service.SensorsService;
import cn.house.houseai.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author 羡y
 * @date 2024/9/30
 * @Description
 */
@RestController("SensorsController")
@RequestMapping("/user/sensors")
@Api(tags = "C端-传感器相关接口")
@Slf4j
@CrossOrigin
public class SensorsController {
    @Autowired
    private SensorsService sensorsService;

    @GetMapping("/{deviceId}")
    @ApiOperation(value = "用户查询设备的传感器")
    public ResultVo getSensorsByUserId(@PathVariable Integer deviceId) {
        return sensorsService.querySensorsByUserId(deviceId);
    }

    @PostMapping("/insert/{user_id}/{device_id}")
    @ApiOperation(value = "用户为设备添加传感器")
    public ResultVo insert(@PathVariable int user_id,
                           @PathVariable int device_id,
                           @RequestBody Sensors sensors) {
        // 参数校验
        if (user_id <= 0) {
            return new ResultVo(400, null, "无效的用户ID");
        }
        if (device_id <= 0) {
            return new ResultVo(400, null, "无效的设备ID");
        }
        if (sensors == null) {
            return new ResultVo(101, null, "传感器信息不能为空");
        }

        // 调用 service 层插入设备，直接返回实现层的 ResultVo
        return sensorsService.addSensorToUserDevices(user_id, sensors, device_id);
    }

    // 更新传感器
    @PutMapping("/{device_id}/{sensor_id}")
    @ApiOperation(value = "用户为设备修改传感器信息")
    public ResponseEntity<ResultVo> updateSensor(@PathVariable Integer device_id, @PathVariable Integer sensor_id, @RequestBody Sensors sensor) {
        ResultVo result = sensorsService.updateSensors(device_id, sensor_id, sensor);
        return ResponseEntity.ok(result);

    }
    }
